<?php

namespace App\Admin\Controllers;

use App\Admin\Repositories\AdvertiseVideoRepository;
use App\AdvertiserVideo;
use Dcat\Admin\Http\Controllers\AdminController;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
class AdvertiseVideoController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new AdvertiserVideo());
        $grid->model()->where('status', '=', 2)
            ->orderBy('suggestion', 'desc')
            ->orderBy('resource_created_time', 'desc')
            ->with('advertiser');
        $grid->actions(function (Grid\Displayers\Actions $actions) {
            $actions->disableDelete();
            $actions->disableEdit();
            $actions->disableQuickEdit();
            $actions->disableView();
        })->disableActions();

        $grid->disableCreateButton();

        // 这里的字段会自动使用翻译文件
        $grid->column('id')/*->sortable()*/;
        $grid->column('account_id', '广告主帐号');
        $grid->column('advertiser.corporation_name', '广告主名称')->width('125px');
        $grid->column('video_id', '资产id');
        $grid->column('task_id', '易盾taskId');
        $grid->column('suggestion', '审核结果')->filter(
            Grid\Column\Filter\In::make([
                1 => '通过',
                4 => '异常（建议拦截）',
                3 => '疑似（建议人工确认）'
            ])
        )->display(function ($suggestion){
            $suggestionArr = [
                1 => '通过',
                4 => '异常（建议拦截）',
                3 => '疑似（建议人工确认）'
            ];
            return $suggestionArr[$suggestion] ?? '';
        });
        $grid->column('label', '审核备注')->display(function ($label){
            $label = $label ? json_decode($label, true) : [];
            $labelStr = '';
            if (isset($label['video']) && $label['video']) {
                $videoArr = array_column($label['video'], 'tagName');
                $labelStr .= '【视频异常】' . implode(',' , $videoArr) . '<br>';
            }
            if (isset($label['audio']) && $label['audio']){
                $audioArr = array_column($label['audio'], 'tagName');
                $labelStr .= '【音频异常】' . implode(',' , $audioArr) . '<br>';
            }
            return $labelStr;
        });
        $grid->column('preview_url', '视频地址')->display(function ($url) {
            return '<a href="javascript:void(0)" onclick="window.open(' .'\''. $url .'\''. ')">' . "查看" . '</a>';
        });
        $grid->column('resource_created_time', '创建时间')->display(function ($time) {
            return date('Y-m-d H:i:s', $time);
        });

        $grid->filter(function (Grid\Filter $filter) {
            $filter->equal('account_id', '广告主帐号');
            $filter->like('advertiser.corporation_name', '企业名称');
            // 设置time类型
            $filter->between('resource_created_time', '创建时间')->datetime()->toTimestamp();;
        });
        return $grid;
    }

//    /**
//     * Make a show builder.
//     *
//     * @param mixed $id
//     *
//     * @return Show
//     */
//    protected function detail($id)
//    {
//        return Show::make($id, new AdvertiseVideoRepository(), function (Show $show) {
//            // 这里的字段会自动使用翻译文件
//            $show->field('id');
//            $show->field('account_id', '广告主帐号');
//            $show->field('system_status', '状态');
//            $show->field('corporation_name', '企业名称');
//            $show->field('corporate_brand_name', '品牌名称');
//            $show->field('customized_industry', '行业名称');
//        });
//    }
//
//    /**
//     * Make a form builder.
//     *
//     * @return Form
//     */
//    protected function form()
//    {
//        return Form::make(new AdvertiseVideoRepository(), function (Form $form) {
//            // 这里的字段会自动使用翻译文件
//            $form->display('id');
//            $form->text('account_id', '广告主帐号');
//            $form->text('system_status', '状态');
//            $form->text('corporation_name', '企业名称');
//            $form->text('corporate_brand_name', '品牌名称');
//            $form->text('customized_industry', '行业名称');
//        });
//    }
}
